//
//  TTTabBarController.m
//  TTBaseProject
//
//  Created by shny588 on 16/7/8.
//  Copyright © 2016年 tt. All rights reserved.
//

#import "TTTabBarController.h"
#import "TTFirstViewController.h"
#import "TTMoreViewController.h"
#import "TTNavigationViewController.h"

@interface TTTabBarController ()

@property (nonatomic, strong) TTFirstViewController             *firstVC;
@property (nonatomic, strong) UINavigationController            *firstNav;
@property (nonatomic, strong) TTFirstViewController             *secondVC;
@property (nonatomic, strong) UINavigationController            *secondNav;
@property (nonatomic, strong) TTFirstViewController             *thirdVC;
@property (nonatomic, strong) UINavigationController            *thirdNav;
@property (nonatomic, strong) TTFirstViewController             *fourthVC;
@property (nonatomic, strong) UINavigationController            *fourthtNav;
@property (nonatomic, strong) TTMoreViewController              *moreVC;
@property (nonatomic, strong) TTNavigationViewController        *moreNav;

@end

@implementation TTTabBarController

/**
 *  得到当前tabbar
 *
 *  @return 当前程序tabbar
 */
+ (TTTabBarController *)currTabBar{
    TTTabBarController *tabBarController = (TTTabBarController *)[UIApplication sharedApplication].delegate.window.rootViewController;
    if ([tabBarController isKindOfClass:[TTTabBarController class]]){
        return tabBarController;
    }
    return nil;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [[self tabBar] setTranslucent:NO];
        //配置viewControllers
        [self setupViewControllers];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Navigation

#pragma mark - private methods
/**
 * 配置ViewControllers
 */
-(void)setupViewControllers{
    //第一
    self.firstVC = [[TTFirstViewController alloc] init];
    self.firstNav = [[UINavigationController alloc] initWithRootViewController:self.firstVC];
    self.firstNav.navigationBar.translucent = NO;
    UIImage *imTabBarImage = [UIImage imageNamed:@"base_tab_imIcon"];
    UIImage *imTabBarSelectImage = [UIImage imageNamed:@"base_tab_imIconS"];
    self.firstNav.tabBarItem.image = [imTabBarImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.firstNav.tabBarItem.selectedImage = imTabBarSelectImage;
    self.firstVC.title = @"第一";
    
    //第二
    self.secondVC = [[TTFirstViewController alloc] init];
    self.secondNav = [[UINavigationController alloc] initWithRootViewController:self.secondVC];
    self.secondNav.navigationBar.translucent = NO;
    self.secondNav.tabBarItem.image = [imTabBarImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.secondNav.tabBarItem.selectedImage = imTabBarSelectImage;
    self.secondVC.title = @"第二";
    
    //第三
    self.thirdVC = [[TTFirstViewController alloc] init];
    self.thirdNav = [[UINavigationController alloc] initWithRootViewController:self.thirdVC];
    self.thirdNav.navigationBar.translucent = NO;
    self.thirdNav.tabBarItem.image = [imTabBarImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.thirdNav.tabBarItem.selectedImage = imTabBarSelectImage;
    self.thirdVC.title = @"第三";
    
    //第四
    self.fourthVC = [[TTFirstViewController alloc] init];
    self.fourthtNav = [[UINavigationController alloc] initWithRootViewController:self.fourthVC];
    self.fourthtNav.navigationBar.translucent = NO;
    self.fourthtNav.tabBarItem.image = [imTabBarImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.firstNav.tabBarItem.selectedImage = imTabBarSelectImage;
    self.fourthVC.title = @"第四";
    
    //第五
    self.moreVC = [[TTMoreViewController alloc] init];
    self.moreNav = [[TTNavigationViewController alloc] initWithRootViewController:self.moreVC];
    self.moreNav.navigationBar.translucent = NO;
    self.moreNav.tabBarItem.image = [imTabBarImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.firstNav.tabBarItem.selectedImage = imTabBarSelectImage;
    self.moreVC.title = @"我的";
    
    [self setViewControllers:@[_firstNav,
                               _secondNav,
                               _thirdNav,
                               _fourthtNav,
                               _moreNav]];
    
    //tabBar前景色
    self.tabBar.tintColor = [UIColor blackColor];
    //调整标签颜色
    [[UITabBarItem appearance] setTitleTextAttributes:
     [NSDictionary dictionaryWithObjectsAndKeys:[UIColor blueColor],                                                                                                              NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
    self.selectedIndex = 4;
}

@end
